﻿using System;
using System.Web.Mvc;

namespace Avalon.Core.MVC.Attributes
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public sealed class RenderModeAttribute : Attribute, IMetadataAware
    {
        public RenderMode RenderMode { get; set; }

        public RenderModeAttribute(RenderMode renderMode)
        {
            RenderMode = renderMode;
        }

        public void OnMetadataCreated(ModelMetadata metadata)
        {
            metadata.IsReadOnly = false;
            switch (RenderMode)
            {
                case RenderMode.DisplayModeOnly:
                    metadata.ShowForDisplay = true;
                    metadata.ShowForEdit = false;
                    metadata.IsReadOnly = true;
                    break;

                case RenderMode.EditModeOnly:
                    metadata.ShowForDisplay = false;
                    metadata.ShowForEdit = true;
                    metadata.IsReadOnly = false;
                    break;

                case RenderMode.None:
                    metadata.ShowForDisplay = false;
                    metadata.ShowForEdit = false;
                    metadata.IsReadOnly = true;
                    break;

                case RenderMode.ReadOnly:
                    metadata.ShowForDisplay = true;
                    metadata.ShowForEdit = true;
                    metadata.IsReadOnly = true;
                    break;
            }

        }
    }

    public enum RenderMode
    {
        Any,
        EditModeOnly,
        DisplayModeOnly,
        ReadOnly,
        None
    }
}
